package cn.wolfcode.esasticserch.service.impl;

import cn.wolfcode.esasticserch.domain.Product;
import cn.wolfcode.esasticserch.reporistory.ProductReporistory;
import cn.wolfcode.esasticserch.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductReporistory productReporistory;
    @Override
    public void insert(Product product) {
        productReporistory.save(product);
    }

    @Override
    public void delete(String id) {
        productReporistory.deleteById(id);
    }

    @Override
    public void update(Product product) {
        productReporistory.save(product);
    }

    @Override
    public Product selectOne(String id) {
        Optional<Product> byId = productReporistory.findById(id);
        Product product = byId.get();
        return product;
    }

    @Override
    public List<Product> selectAll() {
        Iterable<Product> iterable = productReporistory.findAll();
        List<Product> products=new ArrayList<>();
        iterable.forEach(a->products.add(a));
        return products;
    }
}
